<template>
  <div>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 提示区域 -->
      <el-alert title="添加景区信息" type="info" center show-icon :closable="false"></el-alert>
      <!-- 步骤条区域 -->
      <el-steps :space="250" :active="activeIndex - 0" finish-status="success" align-center>
        <el-step title="景区信息"></el-step>
        <el-step title="其他信息"></el-step>
        <el-step title="景区图片"></el-step>
        <el-step title="景区介绍"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>

      <!-- tab栏区域 -->

      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="100px"
        label-position="top"
      >
        <el-tabs v-model="activeIndex" :tab-position="'left'" :before-leave="beforeTabLeave">
          <el-tab-pane label="景区信息" name="0">
            <el-form-item label="景区名称" prop="name">
              <el-input v-model="addForm.name"></el-input>
            </el-form-item>
            <el-form-item label="景区价格" prop="price">
              <el-input v-model="addForm.price" type="number"></el-input>
            </el-form-item>
            <el-form-item label="景区地址" prop="position">
              <el-input v-model="addForm.position"></el-input>
            </el-form-item>
            <el-form-item label="景区等级" prop="rank">
              <el-select
                v-model="addForm.rank"
                placeholder="景区等级"
                clearable
                style="width: 100%"
                class="filter-item"
              >
                <el-option
                  v-for="item in rank"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="其他信息" name="1">
            <el-form-item label="到达景区的详细交通路线" prop="traffic">
              <el-input v-model="addForm.traffic"></el-input>
            </el-form-item>
            <el-form-item label="景区的开放时间" prop="traffic">
              <el-input v-model="addForm.open"></el-input>
            </el-form-item>
            <el-form-item label="景区联系方式" prop="traffic">
              <el-input v-model="addForm.tel"></el-input>
            </el-form-item>
            <el-form-item label="景区经度" prop="traffic">
              <el-input v-model="addForm.longitude"></el-input>
            </el-form-item>
            <el-form-item label="景区纬度" prop="traffic">
              <el-input v-model="addForm.latitude"></el-input>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="景区图片" name="2">
            <!-- action 表示图片要上传到的后台API地址 -->
            <el-upload
              :action="uploadURL"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              list-type="picture"
              :on-success="handleSuccess"
            >
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </el-tab-pane>
          <el-tab-pane label="景区介绍" name="3">
            <!-- 富文本编辑器组件 -->
            <quill-editor v-model="addForm.content" :options="quillOption"></quill-editor>
            <!-- 添加景点的按钮 -->
            <el-button type="primary" class="btnAdd" @click="add">添加景区信息</el-button>
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>
    <!-- 图片预览 -->
    <el-dialog title="图片预览" :visible.sync="previewVisible" width="50%">
      <img :src="previewPath" alt class="previewImg" />
    </el-dialog>
  </div>
</template>

<script>
import quillConfig from './quill-config.js'

export default {
  data() {
    return {
      quillOption: quillConfig,
      activeIndex: '0',
      rank: [
        {
          value: '0',
          label: '无等级'
        },
        {
          value: '1',
          label: 'A级'
        },
        {
          value: '2',
          label: 'AA级'
        },
        {
          value: '3',
          label: 'AAA级'
        },
        {
          value: '4',
          label: 'AAAA级'
        },
        {
          value: '5',
          label: 'AAAAA级'
        }
      ],
      addForm: {
        name: '',
        price: 0,
        position: '',
        rank: '无等级',
        pics: [],
        content: '',
        traffic: '',
        tel: '',
        open: '',
        src: ''
      },
      addFormRules: {
        name: [{ required: true, message: '请输入景区名称', trigger: 'blur' }],
        price: [{ required: true, message: '请输入景区价格', trigger: 'blur' }],
        position: [
          { required: true, message: '请输入景区地址', trigger: 'blur' }
        ],
        rank: [{ required: true, message: '请选择景区等级', trigger: 'blur' }],
        traffic: [
          { required: true, message: '请输入其他信息', trigger: 'blur' }
        ]
      },
      list: [],
      uploadURL: 'http://127.0.0.1:5000/upload',
      previewPath: '',
      previewVisible: false
    }
  },
  methods: {
    beforeTabLeave(activeName, oldActiveName) {
      // console.log('即将离开的标签页名字是：' + oldActiveName)
      // console.log('即将进入的标签页名字是：' + activeName)
      // return false
      if (oldActiveName === '0' && this.addForm.rank === -1) {
        this.$message.error('请先选择景区等级！')
        return true
      }
    },
    // 处理图片预览效果
    handlePreview(file) {
      // console.log(file)
      this.previewPath = file.response.url
      this.previewVisible = true
    },
    // 处理移除图片的操作
    handleRemove(file) {
      // console.log(file)
      // 1. 获取将要删除的图片的临时路径
      const filePath = file.response.tmp_path
      // 2. 从 pics 数组中，找到这个图片对应的索引值
      const i = this.addForm.pics.findIndex(x => x.pic === filePath)
      // 3. 调用数组的 splice 方法，把图片信息对象，从 pics 数组中移除
      this.addForm.pics.splice(i, 1)
      // console.log(this.addForm)
    },
    // 监听图片上传成功的事件
    handleSuccess(response) {
      // console.log(response)
      // 1. 拼接得到一个图片信息对象
      const picInfo = { pic: response.tmp_path, url: response.url }
      // 2. 将图片信息对象，push 到pics数组中
      this.addForm.pics.push(picInfo)
      console.log(this.addForm)
    },

    // 添加景点
    add() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) {
          return this.$message.error('请填写必要的表单项！')
        }
        this.addForm.pics.forEach(item => {
          this.list.push(item.url)
        })
        this.addForm.src = this.list.join(',')
        console.log(this.addForm)

        // 发起请求添加景点
        // 景点的名称，必须是唯一的
        const { data: res } = await this.$http.post('/scenery', this.addForm)

        if (res.status !== 201) {
          return this.$message.error('添加景点失败！')
        }

        this.$message.success('添加景点成功！')
        this.$router.push('/scenery/list')
      })
    }
  }
}
</script>

<style lang="stylus" scoped>
.el-checkbox {
  margin: 0 10px 0 0 !important;
}

.previewImg {
  width: 100%;
}

.btnAdd {
  margin-top: 15px;
}
</style>
